<%@ page language="java" import="java.sql.*,javax.naming.*,javax.sql.*" pageEncoding="utf-8"%>
<html>
<head>
<script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
	$(document).ready(
			function() {
				$('#refreash').click(function() {
					//alert("refreash...");
                	$(location).attr('href',"index.jsp");
            	});
            	
		$('#search').click(function() {
			alert($("#SkuToSearch").val());
			$.ajaxSetup({
				cache : false
			});
			$("#mytable").find("tr:gt(1)").remove();
			//$.getJSON("qservice.jsp", { Serailkey: $("input[name='Serailkey']:checked").val() }, function(data, state) {
			$.getJSON("servicec.jsp", {

				StorerkeyFromSearch : $("#StorerkeyFromSearch").val(),
				StorerkeyToSearch : $("#StorerkeyToSearch").val(),
				SkuFromSearch : $("#SkuFromSearch").val(),
				SkuToSearch : $("#SkuToSearch").val(),
				ClassFromSearch : $("#ClassFromSearch").val(),
				ClassToSearch : $("#ClassToSearch").val(),
				ZoneFromSearch : $("#ZoneFromSearch").val(),
				ZoneToSearch : $("#ZoneToSearch").val(),
				LocFromSearch : $("#LocFromSearch").val(),
				LocToSearch : $("#LocToSearch").val()

			}, function(data, state) {
				//alert(data);
				$.each(data.jsonChildarray, function(i, data) {
					alert(data.StorerkeyFrom + data.StorerkeyTo);
					$("#mytable").append(
							'<tr><td><input type="radio" name="Serailkey" value="' + data.Serailkey + '" /></td><td>' 
							+ data.StorerkeyFrom + '</td><td>' 
							+ data.StorerkeyTo + '</td><td>' 
							+ data.SkuFrom + '</td><td>' 
							+ data.SkuTo + '</td><td>' 
							+ data.ClassFrom + '</td><td>' 
							+ data.ClassTo + '</td><td>' 
							+ data.ZoneFrom + '</td><td>' 
							+ data.ZoneTo + '</td><td>' 
							+ data.LocFrom + '</td><td>' 
							+ data.LocTo + '</td>'
							+ '</tr>');
							});
							/******************/
								
			});
			
			return false;
		});

		$('#add').click(function() {
			$("#CCParameterName").val("CCParameterName");
			$("#StorerkeyFrom").val("0");
			$("#StorerkeyTo").val("ZZZZZZZZZZ");
			$("#SkuFrom").val("0");
			$("#SkuTo").val("ZZZZZZZZZZ");
			$("#ClassFrom").val("0");
			$("#ClassTo").val("ZZZZZZZZZZ");
			$("#ZoneFrom").val("0");
			$("#ZoneTo").val("ZZZZZZZZZZ");
			$("#LocFrom").val("0");
			$("#LocTo").val("ZZZZZZZZZZ");
			$("#XCOORDFROM").val("0");
			$("#XCOORDTO").val("999999");
			$("#YCOORDFROM").val("0");
			$("#YCOORDTO").val("999999");
			$("#ZCOORDFROM").val("0");
			$("#ZCOORDTO").val("999999");

			$("div").hide(1);

			//alert("CC checked?");
			$("#CC").attr('checked', true);
			//alert("CC checked?" + $("#CC").val());
			//$("#CC").checked = true;
			//alert("CC checked?"+ $("#CC").val());

			//alert($("input[name='Serailkey']:checked").val());
			$("input[name='Serailkey']:checked").attr('checked', false);
			//alert($("input[name='Serailkey']:checked").val());
			//$("input[name='Serailkey']:checked").removeAttr("checked");
		});

		//$('#Serailkey').change(function() {
		//$("input[@name='Serailkey']").change(function(){
		$("input[name='Serailkey']:radio").change(function() {
			//alert($("input[name='Serailkey']:checked").val());

			$.ajaxSetup({
				cache : false
			});
			//$.getJSON("qservice.jsp", { Serailkey: $("input[name='Serailkey']:checked").val() }, function(data, state) {
			$.getJSON("query.jsp", {
				Serailkey : $("input[name='Serailkey']:checked").val()
			}, function(data, state) {

				/*****************debug/
				alert("data: " + data.StorerkeyFrom);
				/******************/

				$("#CCParameterName").val(data.CCParameterName);
				$("#StorerkeyFrom").val(data.StorerkeyFrom);
				$("#StorerkeyTo").val(data.StorerkeyTo);
				$("#SkuFrom").val(data.SkuFrom);
				$("#SkuTo").val(data.SkuTo);
				$("#ClassFrom").val(data.ClassFrom);
				$("#ClassTo").val(data.ClassTo);
				$("#ZoneFrom").val(data.ZoneFrom);
				$("#ZoneTo").val(data.ZoneTo);
				$("#LocFrom").val(data.LocFrom);
				$("#LocTo").val(data.LocTo);
				$("#XCOORDFROM").val(data.XCOORDFROM);
				$("#XCOORDTO").val(data.XCOORDTO);
				$("#YCOORDFROM").val(data.YCOORDFROM);
				$("#YCOORDTO").val(data.YCOORDTO);
				$("#ZCOORDFROM").val(data.ZCOORDFROM);
				$("#ZCOORDTO").val(data.ZCOORDTO);
				if (data.DisplaySystemCount == 1) {
					$("#DisplaySystemCount").attr('checked', true);
				} else {
					$("#DisplaySystemCount").attr('checked', false);
				}
				$("#CC").attr('checked', true);

				/******************/
				//if ( k == -1 ){}
			});

		});

	});//$(document).ready	

	function Search() {
		$("#myform").attr("action", "index.jsp");
		$("#myform").submit();
	}
	function save() {
		$("#myform").attr("action", "save.jsp");
		$("#myform").submit();
	}
	function del() {
		$("#myform").attr("action", "del.jsp");
		$("#myform").submit();
	}

	function getURLParameter(name) {
		return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(
				Location.search) || [ , null ])[1]);
	}

	$.fn.clearForm = function() {
		// iterate each matching form
		return this.each(function() {
			// iterate the elements within the form
			$(':input', this).each(function() {
				var type = this.type, tag = this.tagName.toLowerCase();
				if (type == 'text' || type == 'password' || tag == 'textarea')
					this.value = '';
				else if (type == 'checkbox' || type == 'radio')
					this.checked = false;
				else if (tag == 'select')
					this.selectedIndex = -1;
			});
		});
	};
</script>
</head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">


<body>
<%
			String status = "";
			if("ok".equals(request.getParameter("status"))){
				String action = request.getParameter("action");
				if("save".equals(action)){
					status = "已成功保存";
				}
				if("del".equals(action)){
					status = "已成功刪除";
				}
				if("pub".equals(action)){
					status = "已成功發佈";
				}
			}
%>

	<form id='myform' action="updatetable.jsp" method="post" ALIGN="CENTER" >
		<table ALIGN="CENTER"><tr>
			<td Width="100"><input type="button" id="add" value="新增" /></td>
			<td Width="100"><input type="button" onclick="save()" value="保存" /></td>
			<td Width="100"><input type="button" onclick="del()" value="刪除" /></td>
			<td Width="100"><input type="button" id="refreash" value="刷新" /></td>
			<td Width="100"></td>
			<td Width="100"></td>
			<td Width="100"><div> <% out.print(status); %></div></td>
			<td Width="100"></td>
			<td Width="100"><input type="hidden" id="ACT" name="ACT" value="" /></td>
		</tr>
		</table>
		
		<table id="mytable"  border = 1 ALIGN="CENTER">
			<tr>
				<td Width="10"><input type="image" onclick="Search()" src="../icon/zoom.jpg" width="20" height="20" alt="search" /></td>
				<td Width="100"><input type="text" name="StorerkeyFromSearch" id="StorerkeyFromSearch" value="<%=request.getParameter("StorerkeyFromSearch")==null?"":request.getParameter("StorerkeyFromSearch")%>" /></td>
				<td Width="100"><input type="text" name="StorerkeyToSearch" id="StorerkeyToSearch" value="<%=request.getParameter("StorerkeyToSearch")==null?"":request.getParameter("StorerkeyToSearch")%>" /></td>
				<td Width="100"><input type="text" name="SkuFromSearch" id="SkuFromSearch" value="<%=request.getParameter("SkuFromSearch")==null?"":request.getParameter("SkuFromSearch")%>" /></td>
				<td Width="100"><input type="text" name="SkuToSearch" name="SkuToSearch" value="<%=request.getParameter("SkuToSearch")==null?"":request.getParameter("SkuToSearch")%>" /></td>
				<td Width="100"><input type="text" name="ClassFromSearch" id="ClassFromSearch" value="<%=request.getParameter("ClassFromSearch")==null?"":request.getParameter("ClassFromSearch")%>" /></td>
				<td Width="100"><input type="text" name="ClassToSearch" id="ClassToSearch" value="<%=request.getParameter("ClassToSearch")==null?"":request.getParameter("ClassToSearch")%>" /></td>
				<td Width="100"><input type="text" name="ZoneFromSearch" id="ZoneFromSearch" value="<%=request.getParameter("ZoneFromSearch")==null?"":request.getParameter("ZoneFromSearch")%>" /></td>
				<td Width="100"><input type="text" name="ZoneToSearch" id="ZoneToSearch" value="<%=request.getParameter("ZoneToSearch")==null?"":request.getParameter("ZoneToSearch")%>" /></td>
				<td Width="100"><input type="text" name="LocFromSearch" id="LocFromSearch" value="<%=request.getParameter("LocFromSearch")==null?"":request.getParameter("LocFromSearch")%>" /></td>
				<td Width="100"><input type="text" name="LocToSearch" id="LocToSearch" value="<%=request.getParameter("LocToSearch")==null?"":request.getParameter("LocToSearch")%>" /></td>
			</tr>
			<tr>
				<th Width="10"></th>
				<th Width="100">貨主起</th>
				<th Width="100">貨主訖</th>
				<th Width="100">貨品起</th>
				<th Width="100">貨品訖</th>
				<th Width="100">品類大項起</th>
				<th Width="100">品類大項訖</th>
				<th Width="100">區起</th>
				<th Width="100">區訖</th>
				<th Width="100">儲位起</th>
				<th Width="100">儲位訖</th>
			</tr>
			
			<% Connection conn = null;
			PreparedStatement pstmt = null;
			String StorerkeyFromSearch = request.getParameter("StorerkeyFromSearch");
		String StorerkeyToSearch = request.getParameter("StorerkeyToSearch");
		String SkuFromSearch = request.getParameter("SkuFromSearch");
		//String SkuToSearch = request.getParameter("SkuToSearch");
		String SkuToSearch = "";
		String ClassFromSearch = request.getParameter("ClassFromSearch");
		String ClassToSearch = request.getParameter("ClassToSearch");
		String ZoneFromSearch = request.getParameter("ZoneFromSearch");
		String ZoneToSearch = request.getParameter("ZoneToSearch");
		String LocFromSearch = request.getParameter("LocFromSearch");
		String LocToSearch = request.getParameter("LocToSearch");
		
		
		/***************debug*/
		System.out.println("StorerkeyFromSearch:" + StorerkeyFromSearch);
		System.out.println("StorerkeyToSearch:" + StorerkeyToSearch);
		System.out.println("SkuFromSearch:" + SkuFromSearch);
		System.out.println("SkuToSearch:" + SkuToSearch);
		System.out.println("ClassFromSearch:" + ClassFromSearch);
		System.out.println("ClassToSearch:" + ClassToSearch);
		System.out.println("ZoneFromSearch:" + ZoneFromSearch);
		System.out.println("ZoneToSearch:" + ZoneToSearch);
		System.out.println("LocFromSearch:" + LocFromSearch);
		System.out.println("LocToSearch:" + LocToSearch);
		/****************/
	
		String StorerkeyFrom_q = "0";
		String StorerkeyTo_q = "ZZZZZZZZZZZZZZZ";
		String SkuFrom_q = "0";
		String SkuTo_q = "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
		String ClassFrom_q = "0";
		String ClassTo_q = "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
		String ZoneFrom_q = "0";
		String ZoneTo_q = "ZZZZZZZZZZ";
		String LocFrom_q = "0";
		String LocTo_q = "ZZZZZZZZZZ";

		/*
		if(StorerkeyFromSearch.trim() !="") StorerkeyFrom_q = StorerkeyFromSearch.trim();
		if( StorerkeyToSearch.trim() !="") StorerkeyTo_q = StorerkeyToSearch.trim();
		if( SkuFromSearch.trim() !="") SkuFrom_q = SkuFromSearch.trim();
		if( SkuToSearch.trim() !="") SkuTo_q = SkuToSearch.trim();
		if( ClassFromSearch.trim() !="") ClassFrom_q = ClassFromSearch.trim();
		if( ClassToSearch.trim() !="") ClassTo_q = ClassToSearch.trim();
		if( ZoneFromSearch.trim() !="") ZoneFrom_q = ZoneFromSearch.trim();
		if( ZoneToSearch.trim() !="") ZoneTo_q = ZoneToSearch.trim();
		if( LocFromSearch.trim() !="") LocFrom_q = LocFromSearch.trim();
		if( LocToSearch.trim() !="") LocTo_q = LocToSearch.trim();
		*/
		
		if((StorerkeyFromSearch !="") && (StorerkeyFromSearch != null)) StorerkeyFrom_q = StorerkeyFromSearch;
		if((StorerkeyToSearch !="") && (StorerkeyToSearch != null)) StorerkeyTo_q = StorerkeyToSearch;
		if((SkuFromSearch !="") && (SkuFromSearch != null)) SkuFrom_q = SkuFromSearch;
		if((SkuToSearch !="") && (SkuToSearch != null)) SkuTo_q = SkuToSearch;
		if((ClassFromSearch !="") && (ClassFromSearch != null)) ClassFrom_q = ClassFromSearch;
		if((ClassToSearch !="") && (ClassToSearch != null)) ClassTo_q = ClassToSearch;
		if((ZoneFromSearch !="") && (ZoneFromSearch != null)) ZoneFrom_q = ZoneFromSearch;
		if((ZoneToSearch !="") && (ZoneToSearch != null)) ZoneTo_q = ZoneToSearch;
		if((LocFromSearch !="") && (LocFromSearch != null)) LocFrom_q = LocFromSearch;
		if((LocToSearch !="") && (LocToSearch != null)) LocTo_q = LocToSearch;
  		
  		/***************debug*/
		System.out.println("StorerkeyFrom_q:" + StorerkeyFrom_q);
		System.out.println("StorerkeyTo_q:" + StorerkeyTo_q);
		System.out.println("SkuFrom_q:" + SkuFrom_q);
		System.out.println("SkuTo_q:" + SkuTo_q);
		System.out.println("ClassFrom_q:" + ClassFrom_q);
		System.out.println("ClassTo_q:" + ClassTo_q);
		System.out.println("ZoneFrom_q:" + ZoneFrom_q);
		System.out.println("ZoneTo_q:" + ZoneTo_q);
		System.out.println("LocFrom_q:" + LocFrom_q);
		System.out.println("LocTo_q:" + LocTo_q);
		/****************/
    	
    	String sql = "SELECT Serailkey, CCParameterName, StorerkeyFrom, StorerkeyTo , SkuFrom, SkuTo, ClassFrom, ClassTo, ZoneFrom, ZoneTo, LocFrom, LocTo, XCOORDFROM, XCOORDTO, YCOORDFROM, YCOORDTO, ZCOORDFROM, ZCOORDTO, DisplaySystemCount " 
    	+ " FROM CCParameter where StorerkeyFrom >= ? and StorerkeyTo <= ? and SkuFrom >= ? and SkuTo <= ? and ClassFrom >= ? and ClassTo <= ? and ZoneFrom >= ? and ZoneTo <= ? and LocFrom >= ? and LocTo <= ?  order by Serailkey";
    	
    	try {
		Class.forName("oracle.jdbc.driver.OracleDriver");

		Context ctx = new InitialContext();
		DataSource ds = (DataSource) ctx.lookup("java:jdbc/WMPRD_WMWHSE1");
		conn = ds.getConnection();

	
	    	pstmt = conn.prepareStatement(sql);
	    	pstmt.setString(	1	 ,	StorerkeyFrom_q	);
			pstmt.setString(	2	 ,	StorerkeyTo_q	);
			pstmt.setString(	3	 ,	SkuFrom_q	);
			pstmt.setString(	4	 ,	SkuTo_q	);
			pstmt.setString(	5	 ,	ClassFrom_q	);
			pstmt.setString(	6	 ,	ClassTo_q	);
			pstmt.setString(	7	 ,	ZoneFrom_q	);
			pstmt.setString(	8	 ,	ZoneTo_q	);
			pstmt.setString(	9	 ,	LocFrom_q	);
			pstmt.setString(	10	 ,	LocTo_q	);
			
			ResultSet rs = pstmt.executeQuery();
			boolean firstrecord = true;
			while (rs.next()){
				%>
				<tr>
				<td><input type="radio" name="Serailkey" value="<%=rs.getString("Serailkey")%>" /></td>
				<td><%=rs.getString("StorerkeyFrom") %></td>
				<td><%=rs.getString("StorerkeyTo") %></td>
				<td><%=rs.getString("SkuFrom") %></td>
				<td><%=rs.getString("SkuTo") %></td>
				<td><%=rs.getString("ClassFrom") %></td>
				<td><%=rs.getString("ClassTo") %></td>
				<td><%=rs.getString("ZoneFrom") %></td>
				<td><%=rs.getString("ZoneTo") %></td>
				<td><%=rs.getString("LocFrom") %></td>
				<td><%=rs.getString("LocTo") %></td>
				</tr>
			
				<%
			
				
			}
			conn.close();
			} catch (SQLException sqle) {
		out.println("SQL Exception : " + sqle);
		System.out.println("SQL Exception : " + sqle);
	}
			
			%>
			
			
		</table>
		<BR />
		<BR />
		<table id="detail" ALIGN="CENTER" RULES=NONE FRAME=BOX>
			<tr>
				<td Width="100" COLSPAN=2>循環盤點參數名稱</td>
				<td Width="100"><input id="CCParameterName" type="text" name="CCParameterName" /></td>
				<td Width="100"></td>
				<td Width="100"></td>
				<td Width="100"></td>
				
			</tr>
			<tr>
				<td Width="100"></td>
				<td Width="100">起</td>
				<td Width="100"></td>
				<td Width="100">訖</td>
				<td Width="100"></td>
				<td Width="100"></td>
				
			</tr>
			<tr>
				<td Width="100">貨主</td>
				<td Width="100"><input id="StorerkeyFrom" type="text" name="StorerkeyFrom" /></td>
				<td Width="100"></td>
				<td Width="100"><input id="StorerkeyTo" type="text" name="StorerkeyTo" /></td>
				<td Width="100"></td>
				<td Width="150" ROWSPAN=3 >
				<input type="radio" id = "CC" name="type" value="CC" />循環盤點<br />
				<input type="radio" name="type" value="ITRN" />異動盤點<br />
				<input type="radio" name="type" value="EMPTY" />空庫位盤點<br />
				</td>
			</tr>
			<tr>
				<td Width="100">貨品</td>
				<td Width="100"><input id="SkuFrom" type="text" name="SkuFrom" /></td>
				<td Width="100"></td>
				<td Width="100"><input id="SkuTo" type="text" name="SkuTo" /></td>
				<td Width="100"></td>
			</tr>
			<tr>
				<td Width="100">品類大項</td>
				<td Width="100"><input id="ClassFrom" type="text" name="ClassFrom" /></td>
				<td Width="100"></td>
				<td Width="100"><input id="ClassTo" type="text" name="ClassTo" /></td>
				<td Width="100"></td>
			</tr>
			<tr>
				<td Width="100">區</td>
				<td Width="100"><input id="ZoneFrom" type="text" name="ZoneFrom" /></td>
				<td Width="100"></td>
				<td Width="100"><input id="ZoneTo" type="text" name="ZoneTo" /></td>
				<td Width="100"></td>
				<td Width="100"></td>
			</tr>
			<tr>
				<td Width="100">儲位</td>
				<td Width="100"><input id="LocFrom" type="text" name="LocFrom" /></td>
				<td Width="100"></td>
				<td Width="100"><input id="LocTo" type="text" name="LocTo" /></td>
				<td Width="100"></td>
				<td Width="100"></td>
			</tr>
			<tr>
				<td Width="100">X坐標</td>
				<td Width="100"><input id="XCOORDFROM" type="text" name="XCOORDFROM" /></td>
				<td Width="100"></td>
				<td Width="100"><input id="XCOORDTO" type="text" name="XCOORDTO" /></td>
				<td Width="100"></td>
				<td Width="100"></td>
			</tr>
			<tr>
				<td Width="100">Y坐標</td>
				<td Width="100"><input id="YCOORDFROM" type="text" name="YCOORDFROM" /></td>
				<td Width="100"></td>
				<td Width="100"><input id="YCOORDTO" type="text" name="YCOORDTO" /></td>
				<td Width="100"></td>
				<td Width="100"></td>
			</tr>
			<tr>
				<td Width="100">Z坐標</td>
				<td Width="100"><input id="ZCOORDFROM" type="text" name="ZCOORDFROM" /></td>
				<td Width="100"></td>
				<td Width="100"><input id="ZCOORDTO" type="text" name="ZCOORDTO" /></td>
				<td Width="100"></td>
				<td Width="100"></td>
			</tr>
		<tr><td Width="100">顯示系統量<input type="checkbox" id ="DisplaySystemCount" name="DisplaySystemCount" value="1" /><br /></td>
		<td Width="100"></td>
		<td Width="100"></td>
		<td Width="100"></td>
		<td Width="100"></td>
		<td Width="100"></td>
		</tr>
		<tr>
		<td Width="100"></td>
		<td Width="100"></td>
		<td Width="100"></td>
		<td Width="100"><div id="submit" ><input type="submit" value="發佈" /></div></td>
		<td Width="100"></td>
		<td Width="100"></td>
		</tr>
		</table>
	</form>
</body>
</html>

